package com.netcracker.ui.command;

import com.netcracker.entity.Right;
import com.netcracker.util.service.GenreService;
import com.netcracker.util.service.ServiceFacade;
import com.netcracker.util.service.exception.DataTransmittingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.LoggerFactory;

public class DeleteGenreCommand implements Command {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DeleteGenreCommand.class);
    private final ServiceFacade serviceFacade;

    public DeleteGenreCommand(ServiceFacade serviceFacade) {
        this.serviceFacade = serviceFacade;
    }

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) {
        Right right = (Right) request.getSession().getAttribute("right");
        Integer genreID = Integer.parseInt("genreID");
        if (right != null && right.equals(Right.ADMIN)) {
            GenreService genreService = serviceFacade.getGenreService();
            try {
                genreService.deleteGenre(genreID);
                return "?action=allGenres";
            } catch (DataTransmittingException ex) {
                request.setAttribute("error", "Ошибка обращения к БД.");
                LOGGER.error("DB error: ", ex);
                return "index.jsp";
            }
        } else {
            request.setAttribute("error", "Недостаточно прав на удаление жанра.");
            return "index.jsp";
        }
    }
}
